package com.example.lcl.exp2019_3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //动画对象
    private CustomView view;

    //手势分析对象
    private GestureDetector gestureDetector=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        //创建自定义的View对象
        view=new CustomView(this);
        //设置Activity的显示内容为刚创建的View对象
        setContentView(view);

        //创建手势分析对象--同时设定手势识别监听器，用于监听识别结果,参数--Context，OnGestureListener()
        gestureDetector=new GestureDetector(this, new  GestureScaleListener());

        //View必须设置longClickable为true，否则手势识别无法正确工作
        view.setLongClickable(true);

        //为ImageView添加触摸监听处理
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //return true; //此处必须返回true，才能接收处理移动/抬起事件
                return gestureDetector.onTouchEvent(event); //此处不作任何触摸处理，转去手势识别对象进行手势分析
            }
        });
    }

    //手势识别的监听器类--监听手势识别对象的识别结果
    class GestureScaleListener implements GestureDetector.OnGestureListener {

        // 用户轻触触摸屏，由1个MotionEvent ACTION_DOWN触发Java代码
        @Override
        public boolean onDown(MotionEvent arg0) {
            System.out.println("onDown");
            return false;
        }

        //用户按下触摸屏、快速移动后松开，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            System.out.println("onFling");

            String s="";
            if (e1.getY() - e2.getY() > 30 && Math.abs(velocityY) > 50) { //速度和距离都超过一定数值时才算滑动
                // Fling up
                s="上滑";
                ScaleAnimation animation=new ScaleAnimation( 1.0f,  2f,   1.0f,  2f,
                        Animation.RELATIVE_TO_SELF,   0.5f,
                        Animation.RELATIVE_TO_SELF,   0.5f);
                animation.setDuration(1000);
                //设置加速模式
                Interpolator i=new DecelerateInterpolator();
                animation.setInterpolator(i);
                view.startAnimation(animation);
            }
            else if (e2.getY() - e1.getY() > 30 && Math.abs(velocityY) > 50) {
                // Fling down
                s = "下滑";
                ScaleAnimation animation=new ScaleAnimation( 1.0f,  0.3f,   1.0f,  0.3f,
                        Animation.RELATIVE_TO_SELF,   0.5f,
                        Animation.RELATIVE_TO_SELF,   0.5f);
                animation.setDuration(1000);
                //设置加速模式
                Interpolator i=new DecelerateInterpolator();
                animation.setInterpolator(i);
                view.startAnimation(animation);
            }
            Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            return false;
        }

        // 用户长按触摸屏，由多个MotionEvent ACTION_DOWN触发
        @Override
        public void onLongPress(MotionEvent e) {
            System.out.println("onLongPress");
        }

        // 用户按下触摸屏，并拖动，由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            System.out.println("onScroll");
            return false;
        }

        // 用户轻触触摸屏，尚未松开或拖动，由一个1个MotionEvent ACTION_DOWN触发
        // 注意和onDown()的区别，强调的是没有松开或者拖动的状态
        @Override
        public void onShowPress(MotionEvent e) {
            System.out.println("onShowPress");
        }

        //用户（轻触触摸屏后）松开，由一个1个MotionEvent ACTION_UP触发
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            System.out.println("onSingleTapUp");
            return false;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        return true;
    }
}
